package com.example.a_java.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.a_java.entity.RepaymentInstallment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 还款分期明细Mapper接口
 */
@Mapper
public interface RepaymentInstallmentMapper extends BaseMapper<RepaymentInstallment> {

    /**
     * 根据还款计划ID查询分期明细列表
     *
     * @param planId 还款计划ID
     * @return 分期明细列表
     */
    @Select("SELECT * FROM repayment_installment WHERE plan_id = #{planId} ORDER BY period ASC")
    List<RepaymentInstallment> selectByPlanId(@Param("planId") Long planId);

    /**
     * 更新分期明细状态
     *
     * @param id     分期明细ID
     * @param status 状态
     * @return 受影响行数
     */
    @Update("UPDATE repayment_installment SET status = #{status}, update_time = NOW() WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") String status);

    /**
     * 更新还款信息
     *
     * @param id        分期明细ID
     * @param payDate   还款日期
     * @param payAmount 还款金额
     * @param status    状态
     * @return 受影响行数
     */
    @Update("UPDATE repayment_installment SET pay_date = #{payDate}, pay_amount = #{payAmount}, " +
            "status = #{status}, update_time = NOW() WHERE id = #{id}")
    int updatePaymentInfo(@Param("id") Long id,
                         @Param("payDate") String payDate,
                         @Param("payAmount") BigDecimal payAmount,
                         @Param("status") String status);

    /**
     * 更新逾期信息
     *
     * @param id          分期明细ID
     * @param overdueDays 逾期天数
     * @param penalty     罚息
     * @param status      状态
     * @return 受影响行数
     */
    @Update("UPDATE repayment_installment SET overdue_days = #{overdueDays}, penalty = #{penalty}, " +
            "status = #{status}, update_time = NOW() WHERE id = #{id}")
    int updateOverdueInfo(@Param("id") Long id,
                         @Param("overdueDays") Integer overdueDays,
                         @Param("penalty") BigDecimal penalty,
                         @Param("status") String status);

    /**
     * 查询指定计划下所有已还款的分期明细数量
     *
     * @param planId 计划ID
     * @return 已还款分期数量
     */
    @Select("SELECT COUNT(1) FROM repayment_installment WHERE plan_id = #{planId} AND status = 'paid'")
    int countPaidInstallmentsByPlanId(@Param("planId") Long planId);

    /**
     * 查询指定计划下所有已还款的分期金额总和
     *
     * @param planId 计划ID
     * @return 已还款金额总和
     */
    @Select("SELECT IFNULL(SUM(amount), 0) FROM repayment_installment WHERE plan_id = #{planId} AND status = 'paid'")
    BigDecimal sumPaidAmountByPlanId(@Param("planId") Long planId);

    /**
     * 查询指定计划下一个待还款的分期（按期数升序）
     *
     * @param planId 计划ID
     * @return 待还款分期
     */
    @Select("SELECT * FROM repayment_installment WHERE plan_id = #{planId} AND status = 'pending' ORDER BY period ASC LIMIT 1")
    RepaymentInstallment selectNextRepaymentInstallment(@Param("planId") Long planId);

    /**
     * 计算罚息
     *
     * @param installmentId 分期ID
     * @param overdueDays   逾期天数
     * @return 分期明细
     */
    @Select("SELECT *, (amount * 0.0005 * #{overdueDays}) as penalty FROM repayment_installment WHERE id = #{installmentId}")
    RepaymentInstallment calculatePenalty(@Param("installmentId") Long installmentId, @Param("overdueDays") Integer overdueDays);
} 